package org.opentcs.kernel.controlcenter;

import java.awt.event.ItemEvent;
import java.util.ResourceBundle;
import java.util.logging.Logger;
import javax.annotation.Nullable;
import javax.swing.JDialog;

/**
 * A dialog for the user to choose a model to load and the mode the kernel
 * should be set to.
 * 
 * @author Philipp Seifert (Fraunhofer IML)
 * @author Tobais Marquardt (Fraunhofer IML)
 */
public class ChooseModelDialog
    extends JDialog {

  /**
   * This class's logger.
   */
  private static final Logger log =
      Logger.getLogger(ChooseModelDialog.class.getName());
  /**
   * Flag whether user wants to stay in modeling mode or not.
   */
  private boolean modelingMode = true;
  /**
   * Flag whether user has selected the saved model instead of a new one.
   */
  private boolean savedModel = true;
  /**
   * Name of the saved model that can be selected in the dialog.
   */
  private final String model;
  /**
   * The ResourceBundle.
   */
  private static final ResourceBundle bundle =
      ResourceBundle.getBundle("org/opentcs/kernel/controlcenter/Bundle");

  /**
   * Creates new ChooseModelDialog.
   * 
   * @param model Optional name of the saved model. If there is no model,
   * the option to load a saved model will be disabled.
   */
  public ChooseModelDialog(@Nullable String model) {
    this.model = model;
    initComponents();
    setLocationRelativeTo(null);

    boolean modelPresent = model != null;
    savedModelButton.setEnabled(modelPresent);
    savedModelButton.setSelected(modelPresent);
    kernelModePanel.setEnabled(modelPresent);
    modellingRadioButton.setEnabled(modelPresent);
    operatingRadioButton.setEnabled(modelPresent);
    operatingRadioButton.setSelected(modelPresent);
  }

  /**
   * Returns if the user has selected the saved model.
   * 
   * @return boolean True if yes.
   */
  public boolean savedModelSelected() {
    return savedModel;
  }

  /**
   * Returns if the user wants to stay in modeling mode.
   * 
   * @return True if yes, false otherwise.
   */
  public boolean modelingSelected() {
    return modelingMode;
  }
  
  // CHECKSTYLE:OFF
  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {
    java.awt.GridBagConstraints gridBagConstraints;

    kernelModeButtonGroup = new javax.swing.ButtonGroup();
    modelButtonGroup = new javax.swing.ButtonGroup();
    modelPanel = new javax.swing.JPanel();
    newModelButton = new javax.swing.JRadioButton();
    savedModelButton = new javax.swing.JRadioButton();
    kernelModePanel = new javax.swing.JPanel();
    modellingRadioButton = new javax.swing.JRadioButton();
    operatingRadioButton = new javax.swing.JRadioButton();
    buttonPanel = new javax.swing.JPanel();
    okButton = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("org/opentcs/kernel/controlcenter/Bundle"); // NOI18N
    setTitle(bundle.getString("ChooseModelTitle")); // NOI18N
    setMaximumSize(null);
    setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);
    setPreferredSize(null);
    setResizable(false);

    modelPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("ChooseModel"))); // NOI18N
    modelPanel.setLayout(new javax.swing.BoxLayout(modelPanel, javax.swing.BoxLayout.Y_AXIS));

    modelButtonGroup.add(newModelButton);
    newModelButton.setSelected(true);
    newModelButton.setText(bundle.getString("newEmptyModel")); // NOI18N
    newModelButton.addItemListener(new java.awt.event.ItemListener() {
      public void itemStateChanged(java.awt.event.ItemEvent evt) {
        newModelButtonItemStateChanged(evt);
      }
    });
    modelPanel.add(newModelButton);

    modelButtonGroup.add(savedModelButton);
    savedModelButton.setText(bundle.getString("savedModel")); // NOI18N
    savedModelButton.setEnabled(false);
    if(this.model != null) {
      String buttonText = new StringBuilder().append(bundle.getString("savedModel")).append(" (").append(model).append(")").toString();
      savedModelButton.setText(buttonText);
    }
    modelPanel.add(savedModelButton);

    getContentPane().add(modelPanel, java.awt.BorderLayout.NORTH);

    kernelModePanel.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("ChooseKernelMode"))); // NOI18N
    kernelModePanel.setEnabled(false);
    kernelModePanel.setLayout(new javax.swing.BoxLayout(kernelModePanel, javax.swing.BoxLayout.Y_AXIS));

    kernelModeButtonGroup.add(modellingRadioButton);
    modellingRadioButton.setSelected(true);
    modellingRadioButton.setText(bundle.getString("Modelling")); // NOI18N
    modellingRadioButton.setEnabled(false);
    modellingRadioButton.setFocusCycleRoot(true);
    kernelModePanel.add(modellingRadioButton);

    kernelModeButtonGroup.add(operatingRadioButton);
    operatingRadioButton.setText(bundle.getString("Operating")); // NOI18N
    operatingRadioButton.setEnabled(false);
    operatingRadioButton.setFocusCycleRoot(true);
    kernelModePanel.add(operatingRadioButton);

    getContentPane().add(kernelModePanel, java.awt.BorderLayout.CENTER);

    buttonPanel.setLayout(new java.awt.GridBagLayout());

    okButton.setText("OK");
    okButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        okButtonActionPerformed(evt);
      }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
    buttonPanel.add(okButton, gridBagConstraints);

    getContentPane().add(buttonPanel, java.awt.BorderLayout.SOUTH);

    pack();
  }// </editor-fold>//GEN-END:initComponents

  private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
    savedModel = savedModelButton.isSelected();
    modelingMode = modellingRadioButton.isSelected();
    dispose();
  }//GEN-LAST:event_okButtonActionPerformed

  private void newModelButtonItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_newModelButtonItemStateChanged
    if(evt.getStateChange() == ItemEvent.SELECTED) {
      modellingRadioButton.setSelected(true);
      kernelModePanel.setEnabled(false);
      modellingRadioButton.setEnabled(false);
      operatingRadioButton.setEnabled(false);
    }
    else {
      kernelModePanel.setEnabled(true);
      modellingRadioButton.setEnabled(true);
      operatingRadioButton.setEnabled(true);
    }
  }//GEN-LAST:event_newModelButtonItemStateChanged

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JPanel buttonPanel;
  private javax.swing.ButtonGroup kernelModeButtonGroup;
  private javax.swing.JPanel kernelModePanel;
  private javax.swing.ButtonGroup modelButtonGroup;
  private javax.swing.JPanel modelPanel;
  private javax.swing.JRadioButton modellingRadioButton;
  private javax.swing.JRadioButton newModelButton;
  private javax.swing.JButton okButton;
  private javax.swing.JRadioButton operatingRadioButton;
  private javax.swing.JRadioButton savedModelButton;
  // End of variables declaration//GEN-END:variables
  // CHECKSTYLE:ON
}
